package com.zb.order.controller;


import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.SelectVo;
import com.zb.result.Result;
import com.zb.service.ZjWorkBackVisitTemplateFieldService;
import com.zb.service.ZjWorkBackVisitTemplateService;
import com.zb.utils.TokenUtil;
import com.zb.vo.WorkStatementTemplateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 工单回访模板表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-10-08
 */
@Api(tags = "工单回访模板类接口")
@RestController
@RequestMapping("/work/back/visit/template")
public class ZjWorkBackVisitTemplateController {
    @Resource
    private ZjWorkBackVisitTemplateService workBackVisitTemplateService;
    @Resource
    private ZjWorkBackVisitTemplateFieldService workBackVisitTemplateFieldService;

    @SystemLog(moduleType = OperationLogModuleType.WORK_ORDER_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "工单设置-自定义模板-结单模板 - 新增或修改工单费用项目")
    @ApiOperation("新增或修改回访模板")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid WorkStatementTemplateVo templateVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        workBackVisitTemplateService.save(loginInfo, templateVo);
        return Result.success(null == templateVo.getId()? "新增回访模板成功" : "修改回访模板成功");
    }

    @ApiOperation("获取回访模板列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody SelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取回访模板列表成功").data(workBackVisitTemplateService.getList(custAccId, selectVo));
    }

    @ApiOperation("根据回访ID获取回访详情")
    @GetMapping("/getDetail/{id}")
    public Result getDetail(@PathVariable @ApiParam(value = "回访ID", required = true) Long id) {
        return Result.success("获取回访详情成功").data("row", workBackVisitTemplateService.getDetail(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.WORK_ORDER_TYPE, type = OperationLogType.UPDATE, remark = "工单设置-自定义模板-结单模板 - 批量启用或禁用回访模板")
    @ApiOperation("批量启用或禁用回访模板")
    @PutMapping("/disable")
    public Result disable(HttpServletRequest request, @RequestParam @ApiParam(value = "回访状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                          @RequestBody @ApiParam(value = "回访模板ID集合", required = true) List<Long> ids) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        workBackVisitTemplateService.updateDisableBatchById(loginInfo, ids, status);
        return Result.success(status? "禁用回访模板成功" : "启用回访模板成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.WORK_ORDER_TYPE, type = OperationLogType.DELETE, remark = "工单设置-自定义模板-结单模板 - 批量启用或禁用回访模板")
    @ApiOperation("批量删除回访模板")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody @ApiParam(value = "回访模板ID集合", required = true) List<Long> ids) {
        workBackVisitTemplateService.deleteBatchById(ids);
        return Result.success("删除回访模板成功");
    }

    @ApiOperation("根据回访模板自定义字段ID获取字段详情")
    @GetMapping("/getFieldDetail/{id}")
    public Result getFieldDetail(@PathVariable @ApiParam(value = "回访模板自定义字段ID", required = true) Long id) {
        return Result.success("获取字段详情成功").data("row", workBackVisitTemplateFieldService.getFieldDetail(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.WORK_ORDER_TYPE, type = OperationLogType.UPDATE, remark = "工单设置-自定义模板-结单模板 - 批量启用或禁用回访模板自定义字段")
    @ApiOperation("批量启用或禁用回访模板自定义字段")
    @PutMapping("/disableField")
    public Result disableField(@RequestParam @ApiParam(value = "回访模板自定义字段状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                               @RequestBody @ApiParam(value = "回访模板自定义字段ID", required = true) List<Long> ids) {
        workBackVisitTemplateFieldService.updateDisableBatchById(ids, status);
        return Result.success(status? "禁用字段成功" : "启用字段成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.WORK_ORDER_TYPE, type = OperationLogType.UPDATE, remark = "工单设置-自定义模板-结单模板 - 批量启用或禁用回访模板自定义字段")
    @ApiOperation("批量删除回访模板自定义字段")
    @DeleteMapping("/deleteField")
    public Result deleteField(@RequestBody @ApiParam(value = "回访模板自定义字段ID", required = true) List<Long> ids) {
        workBackVisitTemplateFieldService.deleteBatchById(ids);
        return Result.success("删除字段成功");
    }

    @ApiOperation("获取工单模块回访自定义字段列表")
    @PostMapping("/getModuleField/{moduleId}")
    public Result getModuleField(@PathVariable @ApiParam(value = "工单模块ID", required = true) Long moduleId, @RequestBody SelectVo selectVo) {
        return Result.success("获取工单模块回访自定义字段列表成功").data(workBackVisitTemplateService.getModuleField(moduleId, selectVo));
    }

}
